package com.cc.controller;


import com.cc.model.EbProductCategory;
import com.cc.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/productCategory")
public class ProductCategoryController {
    @Autowired
    private ProductCategoryService productCategoryService;

    @RequestMapping("list")
    public ModelAndView list(@RequestParam(value = "page" ,defaultValue = "1") int page,
                             @RequestParam(value = "pageSize" ,defaultValue = "3") int pageSize) {
        List<EbProductCategory> productCategoryServices = productCategoryService.list(page, pageSize);

        int maxPage = productCategoryService.getMaxPage(pageSize);

        ModelAndView mv = new ModelAndView();

        mv.addObject("productCategoryServices", productCategoryServices);
        //绑定当前页
        mv.addObject("curPage", page);
        //b绑定最大页
        mv.addObject("maxPage", maxPage);
        //绑定视图
        mv.setViewName("manage/productClass");

        return mv;
    }
    @RequestMapping("/save")
    public String save(){
        return "manage/productClass-add";
    }
    @RequestMapping("/save-result")
    public String saveResult(EbProductCategory ebProductCategory){
        if(ebProductCategory.getEpc_name()==null||
                ebProductCategory.getEpc_name().equals("")||
                ebProductCategory.getEpc_name().trim().length()<0

                ){
            return "manage/manage-error";

        }
        int result = productCategoryService.save(ebProductCategory);
        if(result<=0){

        }
        return "manage/manage-result";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") int id) {
        int result = productCategoryService.deleteByPk(id);
        System.out.println(result);
        if(result==0){

        }
        return "manage/manage-result1";
    }
    @RequestMapping("/edit")
    public String edit(Model model, Integer epcId){
        EbProductCategory ebProductCategory = productCategoryService.selectByCondition(epcId);
        model.addAttribute("ebProductCategory",ebProductCategory);
        return "manage/productClass-modify";
    }
    @RequestMapping("/edit-result")
    public String editResult(EbProductCategory ebProductCategory) {
        int result = productCategoryService.updateById(ebProductCategory);
        System.out.println(result);
        if(result<=0){
            return "manage/manage-error";
        }
        return "manage/manage-result";
    }

}
